package com.lq.educationsystem.admin.service.impl;


import com.lq.educationsystem.admin.entiy.AdminTb;
import com.lq.educationsystem.admin.mapper.AdminTbMapper;
import com.lq.educationsystem.admin.service.AdminBaseService;
import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.AdminPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminBaseImpl implements AdminBaseService {
    @Autowired
    AdminTbMapper adminTbMapper;
    @Override
    public Result queryAdminById(int adminId) {

        AdminTb adminInfo = adminTbMapper.selectByPrimaryKey((long) adminId);

//        AdminTb adminInfo = adminTbMapper.queryAdminById( adminId);
        //调用服务 获取用户信息
        Result result = ResultBuild.buildOk(new Result<AdminTb>(),adminInfo);
        System.out.println("信息查询结果为:"+ adminInfo.toString());
        return result;
    }

    @Override
    public void insertAdmin(AdminTb adminTb) {
        System.out.println(adminTb.toString());
        //插入用户信息
        int insert = adminTbMapper.insert(adminTb);

    }

    /**
     * 跟新用户信息
     * @param adminTb
     */
    @Override
    public void updateAdminInfo(AdminTb adminTb) {
        int row = adminTbMapper.updateByPrimaryKeySelective(adminTb);

        System.out.println("更新数据为:" +row);
    }

    @Override
    public void deleteAdminInfo(AdminTb adminTb) {
        Integer adminId = adminTb.getAdminId();
        //通过用户id删除用户
        adminTbMapper.deleteByPrimaryKey(Long.valueOf(adminId));
    }

    @Override
    public ResultPage<AdminTb> queryList(AdminPageVo adminPageVo) {

        System.out.println(adminPageVo);
        AdminTb adminInfo = new AdminTb();
        //数据复制
        BeanUtils.copyProperties(adminPageVo,adminInfo);
        System.out.println("adminInfo"+ adminInfo);
        Integer page = adminPageVo.getPage();
        Integer pageSize = adminPageVo.getPageSize();

        //获取分页起始位置
        int start = PageUtil.getStart(page, pageSize);

        //调用查询语句  带模糊查询的语句

        List<AdminTb> adminInfoList=adminTbMapper.queryListByLike(adminInfo,start,pageSize);
        int counts = adminTbMapper.queryAdminCounts(adminInfo);
        System.out.println(adminInfoList);

        return ResultPageBuild.pageBuildOk(new ResultPage<AdminTb>(),page, pageSize,counts,adminInfoList);
    }
}
